package com.wwg.market.ui.member.client.item.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.mobileares.midp.widgets.client.button.ButtonBar;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.pop.IMessageConfirm;
import com.mobileares.midp.widgets.client.pop.Message;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.iprovider.IModelProvider;
import com.nexustar.gwt.widgets.client.ui.iprovider.IPaginationModelProvider;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.member.client.common.MemberUtils;
import com.wwg.market.ui.service.dto.member.MemberDto;
import com.wwg.market.ui.service.interfaces.MemberService;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author gaoyang
 * @version $Id: $
 * @date 12-2-26 下午11:51
 */
public class MemberListTable extends Composite {
    private PaginationTable paginationTable;
    //    private UFishTable<MemberDto> memberListTable;
    //    private IFishModel model;
    private ButtonBar bar;
    private Map paramMap;

    public MemberListTable() {
        FlowPanel panel = new FlowPanel();
        IPaginationModelProvider paginationModelProvider = new IPaginationModelProvider() {
            @Override
            public void setInputData(int begin, int count, final IAsyncModelCallback asyn) {
                Utils.invoke(MemberService.class.getName(), "getMemberList", new Object[]{begin, count, paramMap}, new LoadingAsyncCallback() {
                    @Override
                    protected void success(Object result) {
                        asyn.setModelElments(result);
                    }
                });
            }
        };
        initTable(paginationModelProvider);
//        model = FishModelManager.createTempModel(FishModelManager.TYPE_LIST, new ILoadModelProvider() {
//            public void getModel(Object[] parameters, final IModelCallback iModelCallback) {
//                System.out.println("parameters = " + parameters[0]);
//                Utils.invoke(MemberService.class.getName(), "getMemberList", parameters, new LoadingAsyncCallback() {
//                    @Override
//                    protected void success(Object result) {
//                        iModelCallback.setFishModel(result);
//                    }
//                });
//            }
//        });
//        model.loadByProvider(new Object[]{paraMap});

//        memberListTable.setFishModel((ListModel<MemberDto>) model);
        initActionBar();
        panel.add(bar);
        panel.add(paginationTable);
        initWidget(panel);
    }

    private void initActionBar() {
        bar = new ButtonBar();

        CButton add = new CButton("添加");
        CButton modify = new CButton("编辑");
        CButton delete = new CButton("删除");


        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                //todo:show add form
            }
        });

        modify.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                MemberDto dto = (MemberDto) paginationTable.getTable().getSelectedItem().getUserObject();
                if (dto != null) {
                    //todo:show modify form
                }
            }
        });

        delete.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                MemberDto dto = (MemberDto) paginationTable.getTable().getSelectedItem().getUserObject();
                if (dto != null) {
                    Message.showConfirm("", "确认删除？", new IMessageConfirm() {
                        public void fire(boolean confirm) {
                            if (confirm) {
                                //todo:delete
                                Message.showInfo("", "删除成功！");
                            }
                        }
                    });
                }
            }
        });
        bar.addButton(add);
        bar.addButton(modify);
        bar.addButton(delete);
    }

//    public void setSelectionHandler(final SelectionHandler<TableItem> handler) {
//        memberListTable.getBMUI().addSelectionHandler(new SelectionHandler<TableItem>() {
//            public void onSelection(SelectionEvent<TableItem> tableItemSelectionEvent) {
//                handler.onSelection(tableItemSelectionEvent);
//            }
//        });
//    }

    public void render(Map paramMap) {
        this.paramMap = paramMap;
//        this.paraMap = paramMap;
//        model.loadByProvider(new Object[]{paramMap});
//        memberListTable.getFishModel().render(memberListTable.getFishWidget());
        paginationTable.getModelManger().renderAsyncModel(new Object[]{1, 15});
    }

    private void initTable(IModelProvider provider) {
        ViewTable table = new ViewTable(getColumnWaiter());
        table.addSelectionHandler(new SelectionHandler<TableItem>() {
            public void onSelection(SelectionEvent<TableItem> tableItemSelectionEvent) {
                //todo:
            }
        });
        paginationTable = new PaginationTable(table, 15);
        paginationTable.getModelManger().setProvider(provider);
//        memberListTable = new UFishTable<MemberDto>(table);
//        memberListTable.getBMUI().setWidth("300px");
//        memberListTable.getBMUI().setHeight("300px");
    }

    private ColumnWaiter getColumnWaiter() {
        ColumnExt c1 = new ColumnExt("会员名", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.CheckNull(dto.getUsername()));
            }
        });
        ColumnExt c2 = new ColumnExt("积分", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.CheckNull(dto.getBalance()));
            }
        });

        ColumnExt c3 = new ColumnExt("余额", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.CheckNull(dto.getBalance()));
            }
        });

        ColumnExt c4 = new ColumnExt("状态", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.getLockedString(dto.getLocked()));
            }
        });

        ColumnExt c5 = new ColumnExt("登陆次数", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.CheckNull(dto.getLoginCount()));
            }
        });

        ColumnExt c6 = new ColumnExt("最近登陆时间", 150, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.getDateString(dto.getLastLoginTime()));
            }
        });

        ColumnExt c7 = new ColumnExt("注册时间", 150, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.getDateString(dto.getRegisterTime()));
            }
        });
        ColumnExt c8 = new ColumnExt("创建人", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                MemberDto dto = (MemberDto) cellItem.getUserObject();
                cellItem.setText(MemberUtils.CheckNull(dto.getCreator()));
            }
        });
        return new ColumnWaiter(new ColumnExt[]{c1, c2, c3, c4, c5, c6, c7, c8});
    }

//    public UFishTable<MemberDto> getMemberListTable() {
//        return memberListTable;
//    }
}
